package org.blueoxygen.cimande2.ma.gene;

import java.util.List;

import org.blueoxygen.cimande2.ma.gene.entity.Preferences;
import org.blueoxygen.cimande2.ma.gene.manager.DbManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Preference extends Activity {
	private EditText username, password, connection;
	private Button btnSave;
	private DbManager manager;
	private List<Preferences> preferences;
	private Preferences preference;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.preference);
		TextView listTitle = (TextView) findViewById(R.id.listTitle);
		listTitle.setText("Preference");
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.my_title);

		
		
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
		password.setTransformationMethod(new PasswordTransformationMethod());
		connection = (EditText) findViewById(R.id.connection);
		btnSave = (Button) findViewById(R.id.save);
		manager = new DbManager(this);
		preferences = manager.getDataList();
		if (preferences.size() != 0) {
			for (Preferences p : preferences) {
				preference = new Preferences(p.getUsername(), p.getPassword(),
						p.getConnection());
			}
			username.setText(preference.getUsername());
			password.setText(preference.getPassword());
			connection.setText(preference.getConnection());
		} else {

		}
		btnSave.setOnClickListener(save);
		
		findViewById(R.id.homeBtn).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Preference.this, DashBoard.class);
				startActivity(intent);
			}
		});
		findViewById(R.id.preferenceBtn).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(getIntent());
			}
		});
	}

	private OnClickListener save = new OnClickListener() {
		public void onClick(View v) {
			manager.addData(username.getText().toString(), password.getText()
					.toString(), connection.getText().toString());
			Intent intent = new Intent(Preference.this, DashBoard.class);
			startActivity(intent);
		}
	};
}
